import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class MaxSubstringFinder {

	//TODO add notFoundedLettersMap
	public String findMaxSubstring(List<String> inputStings) {
		if (inputStings == null) { throw new IllegalArgumentException("input List is null");}

		sortStringFromMinToMaxLength(inputStings);
		String shortestString = inputStings.remove(0);
	
		String maxLengthSubstring = "";
		int maxFoundLength = 0;

		for (int startChar = 0; startChar < shortestString.length()	- maxFoundLength; startChar++) {
			for (int endChar = startChar; endChar < shortestString.length(); endChar++) {

				String searchedSubstring = shortestString.substring(startChar,	endChar);

				boolean isNotContains = false;
				for (String string : inputStings) {
					if (!string.contains(searchedSubstring)) {
						isNotContains = true;
						break;
					}
				}
				if (isNotContains) {
					break;
				}

				if (endChar - startChar > maxFoundLength) {
					maxFoundLength = endChar - startChar;
					maxLengthSubstring = searchedSubstring;
				}
			}
		}
		return maxLengthSubstring;
	}

	public void sortStringFromMinToMaxLength(List<String> listOfStrings){
		Collections.sort(listOfStrings, new Comparator<String>() {
			@Override
			public int compare(String str0, String str1) {
				return (str0.length()>str1.length() ? -1 : (str0==str1 ? 0 : 1));
			}
		});
	}
}
